


<template>
  <div id="app">
    <!-- 查询表单 -->
    <!--    欢迎{{this.user.username}}登录-->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="设备编号">
        <el-input v-model="formInline.sbid" placeholder="请输入设备编号"></el-input>
      </el-form-item>

      <el-form-item label="设备类型">
        <el-select v-model="formInline.sid" placeholder="全部"  clearable>
          <el-option  v-for="room in roomList" :label="room.name" :value="room.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="全部" clearable>
          <el-option label="已绑定" value="1"></el-option>
          <el-option label="未绑定" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备唯一码">
        <el-input v-model="formInline.weiyi" placeholder="请输入设备唯一码"></el-input>
      </el-form-item>

      <el-form-item label="主管理员手机号">
        <el-input v-model="formInline.pho" placeholder="请输入移动端手机号码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="findPage()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="info" @click="toAdd()" plain>添加</el-button>
    <!--    <el-button type="primary" @click="daochu()">导出</el-button>-->

    <!--表格用来展示数据-->
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="序号"
      >
      </el-table-column>

      <el-table-column
          prop="sbid"
          label="设备编号"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="tc(scope.row.id)">{{scope.row.sbid}}</el-button>
        </template>
      </el-table-column>

      <el-table-column
          prop="createds"
          label="创建人账号"
      >
      </el-table-column>

      <el-table-column
          prop="weiyi"
          label="设备唯一码"
      >
      </el-table-column>

      <el-table-column
          prop="lxname"
          label="设备类型"
      >
      </el-table-column>

      <el-table-column
          prop="bumen"
          label="所属部门"
      >
      </el-table-column>

      <el-table-column
          prop="yxq"
          label="有效期"
      >
      </el-table-column>

      <el-table-column
          prop="ctime"
          label="创建时间"
      >
      </el-table-column>

      <el-table-column
          prop="pho"
          label="主管理员手机号"
      >
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
      >
        <template slot-scope="scope">
          {{scope.row.status==1?"已绑定":"未绑定"}}
        </template>
      </el-table-column>

      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="toUpdate(scope.row.id)" size="mini">修改</el-button>
          <el-button type="text" v-if="scope.row.status==1" @click="zhuangtai(scope.row.id,2)" size="mini">解绑定</el-button>
          <el-button type="text" @click="yujing(scope.row.id)" size="mini">预警设置</el-button>
<!--          <el-button type="text" v-if="scope.row.status==2" @click="zhuangtai(scope.row.id,1)" size="mini">绑定</el-button>-->
          <el-button type="text" @click="del(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页展示数据-->
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!--添加弹窗-->
    <el-dialog
        title="添加"
        :visible.sync="dialogVisible"
        width="45%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="260px" class="demo-ruleForm">

        <el-form-item label="设备编号:" prop="name">
          <el-input v-model="ruleForm.sbid" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="设备唯一码:" prop="name">
          <el-input v-model="ruleForm.weiyi" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="设备类型:">
          <el-select v-model="ruleForm.sid" placeholder="请选择"  clearable>
            <el-option  v-for="room in roomList" :label="room.name" :value="room.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属部门:">
          <el-select v-model="ruleForm.bumen" placeholder="请选择" clearable>
            <el-option label="数据部" value="数据部"></el-option>
            <el-option label="开发部" value="开发部"></el-option>
            <el-option label="产品部" value="产品部"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="有效期:" prop="yxq">
          <el-radio-group v-model="ruleForm.yxq" style="padding-right: 30px">
            <el-radio label="永久有效">永久有效</el-radio>
            <el-radio label="指定日期"></el-radio>
          </el-radio-group>
            <el-date-picker
                v-if="ruleForm.yxq=='指定日期'"
                style="width: 230px"
                v-model="value1"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd">
            </el-date-picker>
        </el-form-item>

        <el-form-item label="状态:" prop="name">
          <el-tooltip :content="'已绑定是1未绑定是2: ' + ruleForm.status" placement="top">
            <el-switch
                v-model="ruleForm.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="2">
            </el-switch>
          </el-tooltip>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="add('ruleForm')" v-text="ruleForm.id==null?'添加':'修改'"></el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--点击弹窗-->
    <el-dialog
        title="查看"
        :visible.sync="dialogVisible2"
        width="45%">

      <el-form :model="ruleForm2" :rules="rules" ref="ruleForm" label-width="260px" class="demo-ruleForm">

        <el-form-item label="设备编号:" prop="name">
          {{ruleForm2.sbid}}
        </el-form-item>

        <el-form-item label="设备唯一码:" prop="name">
          {{ruleForm2.weiyi}}
        </el-form-item>

        <el-form-item label="设备类型:" prop="name">
          {{ruleForm2.lxname}}
        </el-form-item>

        <el-form-item label="所属部门:" prop="name">
          {{ruleForm2.bumen}}
        </el-form-item>

        <el-form-item label="有效期:" prop="name">
          {{ruleForm2.yxq}}
        </el-form-item>

        <el-form-item label="状态:" prop="name">
          {{ruleForm2.status==1?"已绑定":"未绑定"}}
        </el-form-item>

        <el-form-item label="主管理员手机号:" prop="name">
          {{ruleForm2.pho}}
        </el-form-item>

        <el-button type="primary" @click="guanbi()">关闭</el-button>
      </el-form>
    </el-dialog>

    <!--预警弹窗-->
    <el-dialog
        title="预警设置"
        :visible.sync="dialogVisible3"
        width="35%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
        心率：
        <div class="xl">
          <el-form-item label="状态:" prop="name">
            <el-tooltip :content="'启用是1禁用是2: ' + ruleForm.xlstatus" placement="top">
              <el-switch
                  v-model="ruleForm.xlstatus"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="2">
              </el-switch>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="最低值（bmp）:" prop="name">
            <el-input v-model="ruleForm.xlmin" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="最高值（bmp）:" prop="name">
            <el-input v-model="ruleForm.xlmax" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="监控周期:">
            <el-select v-model="ruleForm.xlzq" placeholder="请选择" clearable>
              <el-option label="5分钟" value="5分钟"></el-option>
              <el-option label="15分钟" value="15分钟"></el-option>
              <el-option label="30分钟" value="30分钟"></el-option>
              <el-option label="1小时" value="1小时"></el-option>
              <el-option label="2小时" value="2小时"></el-option>
              <el-option label="4小时" value="4小时"></el-option>
              <el-option label="8小时" value="8小时"></el-option>
              <el-option label="12小时" value="12小时"></el-option>
              <el-option label="24小时" value="24小时"></el-option>
            </el-select>
          </el-form-item>
        </div>
        血氧：
        <div class="xy">
          <el-form-item label="状态:" prop="name">
            <el-tooltip :content="'启用是1禁用是2: ' + ruleForm.xystatus" placement="top">
              <el-switch
                  v-model="ruleForm.xystatus"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="2">
              </el-switch>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="最低值（%）:" prop="name">
            <el-input v-model="ruleForm.xymin" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="最高值（%）:" prop="name">
            <el-input v-model="ruleForm.xymax" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="监控周期:">
            <el-select v-model="ruleForm.xyzq" placeholder="请选择" clearable>
              <el-option label="5分钟" value="5分钟"></el-option>
              <el-option label="15分钟" value="15分钟"></el-option>
              <el-option label="30分钟" value="30分钟"></el-option>
              <el-option label="1小时" value="1小时"></el-option>
              <el-option label="2小时" value="2小时"></el-option>
              <el-option label="4小时" value="4小时"></el-option>
              <el-option label="8小时" value="8小时"></el-option>
              <el-option label="12小时" value="12小时"></el-option>
              <el-option label="24小时" value="24小时"></el-option>
            </el-select>
          </el-form-item>
        </div>
        步数：
        <div class="bs">
          <el-form-item label="状态:" prop="name">
            <el-tooltip :content="'启用是1禁用是2: ' + ruleForm.bsstatus" placement="top">
              <el-switch
                  v-model="ruleForm.bsstatus"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="2">
              </el-switch>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="最低值（步）:" prop="name">
            <el-input v-model="ruleForm.bsmin" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="最高值（步）:" prop="name">
            <el-input v-model="ruleForm.bsmax" placeholder="请输入"></el-input>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="primary" @click="add('ruleForm')" v-text="ruleForm.id==null?'保存':'修改'"></el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<style >
.xl{
  width: 600px;
  height: 220px;
  //border: 1px solid red;
  margin-left: 100px;
  margin-bottom: 10px;
  line-height: 30px;
  font-size: 14px;
  //text-align: center;
}
.xy{
  width: 600px;
  height: 220px;
  //border: 1px solid red;
  margin-left: 100px;
  margin-bottom: 10px;
  line-height: 30px;
  font-size: 14px;
  //text-align: center;
}
.bs{
  width: 600px;
  height: 200px;
  //border: 1px solid red;
  margin-left: 100px;
  margin-bottom: 10px;
  line-height: 30px;
  font-size: 14px;
  //text-align: center;
}
.el-input{
  width:180px;
  margin-left: 40px;
}
</style>

<script>
// @ is an alias to /src
import axios from "axios"

export default {
  data(){
    return{
      /*
        data:就是用来定义数据的要用什么就自己定义什么
        []:数组，集合
        {}:对象(里面有属性)
         */
      tableData:[],
      pageNum:1,
      pageSize:3,
      total:0,
      formInline:{},
      user:{},
      roomList:[],
      dialogVisible:false,
      dialogVisible2:false,
      dialogVisible3:false,
      tx:[],
      ruleForm:{},
      ruleForm2:{},
      // ruleForm:{},
      imageUrl:"",
      value1:[],
      yid:null,
    }
  },
  methods: {
    /*！！！！！！！！！！！
    methods:这个里面定义的全部都是方法
    现在是spring boot+vue 框架
    不一样的就是在 从vue框架里面的methods里面
    通过方法去请求路径(请求的路径就是后台的路径)
    拿到数据放到下面的created()里面来加载数据才能在页面展示

    ***html使用ajxa 请求controller  controller把数据返回给前端 不再去跳转页面了***

     */
    findPage() { //查询所有加分页参数
      this.axios.post(`http://localhost:9001/sbgl/findPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}`, this.formInline).then(res => {
        console.log(res.data);
        this.tableData = res.data.data.records;
        this.total = res.data.data.total;
        if (this.pageNum>res.data.data.pages){
          this.pageNum=res.data.data.pages;
          this.findPage();
        }
      })
    },
    handleCurrentChange(val) {//分页方法
      this.pageNum = val;
      this.findPage();
    },
    toAdd() { //往添加对话框里清空数据
      this.dialogVisible = true;
      this.ruleForm = {};
    },
    add(formName) { //添加方法
      this.$refs[formName].validate((valid) => {
        if (valid) {//满足校验
          //通过ajax把数据保存到后端
          if (this.ruleForm.yxq == "指定日期"){
            this.ruleForm.yxq = this.value1[0] + "至" + this.value1[1];
          }
          this.axios.post(`http://localhost:9001/sbgl/add`, this.ruleForm).then(res => {
            if (res.data.code == 200) {
              this.$message.success("添加成功");
              this.dialogVisible3 = false;
            } else {
              this.$message.error("添加失败");
            }
            this.dialogVisible = false;
            this.findPage();
          })
        } else {//不满足校验
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) { //重置按钮点击可以重置
      this.$refs[formName].resetFields();
    },
    toUpdate(id) { //修改回显数据方法
      axios.get(`http://localhost:9001/sbgl/findById?id=${id}`).then(res => {
        this.ruleForm = res.data.data;
        this.dialogVisible = true;
      })
    },
    del(id) { //删除方法
      this.$confirm("确定要删除吗?一旦删除,该条信息将不能恢复。", '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get(`http://localhost:9001/sbgl/deleteById?id=${id}`).then(res => {
          if (res.data.code == 200) {
            this.$message.success("删除成功");
          } else {
            this.$message.error("删除失败！");
          }
          this.findPage();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    zhuangtai(id, status) { //状态修改方法
      axios.get(`http://localhost:9001/sbgl/zhuangtai?id=${id}&status=${status}`).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.data);
          this.findPage();
        } else {
          this.$message.error(res.data.data);
        }
      })
    },
    tc(id) {
      this.dialogVisible2 = true;
      this.axios.get(`http://localhost:9001/sbgl/findById?id=${id}`).then(res => {
        this.ruleForm2 = res.data.data;
      })
    },
    guanbi() {
      this.dialogVisible = false;
      this.dialogVisible2 = false;
      this.dialogVisible3 = false;
    },
    xialaList() {
      this.axios.get(`http://localhost:9001/sbgl/xialaList`).then(res => {
        this.roomList = res.data.data;
      })
    },
    yujing(id){
      this.dialogVisible3=true;
      this.axios.get(`http://localhost:9001/sbgl/findById?id=${id}`).then(res => {
        this.ruleForm = res.data.data;
      })
    },
    // add2(){
    //   this.axios.get(`http://localhost:9001/sbgl/yujing?id=${this.yid}`,this.ruleForm).then(res=>{
    //     if (res.data.code == 200) {
    //       this.$message.success(res.data.data);
    //       this.findPage();
    //     } else {
    //       this.$message.error(res.data.data);
    //     }
    //   })
    // },
  },
  created() {
    /*
    ！！！！！！！！！！注意created()：
    这个里面才是可以把数据展示到页面的方法
    思考那个方法是要把数据放到前台 就在这个里面写那个方法
     */
    // var userDB=sessionStorage.getItem("userDB"); //从login.html页面的session域中拿到的数据
    // if (userDB==null){
    //   location.href="#/login";
    // }
    // this.user=JSON.parse(userDB);//动态获取显示登录人的数据 因为从session域中拿出来所以要转换数据类型
    this.findPage();//展示所有的数据
    this.xialaList();//动态获取下拉框的数据
  }
}
</script>
